﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FlightService
{
    /// <summary>
    /// Static class used for converting types
    /// </summary>
    public static class Converter
    {
        /// <summary>
        /// Static method used for converting DataRow objects contained within a DataTable in a DataSet into Flight objects
        /// </summary>
        /// <param name="table">DataTable object to convert</param>
        /// <returns>List of Flight objects, converted from the table</returns>
        public static List<Flight> ToFlights(DataTable table)
        {
            var flights = new List<Flight>();

            foreach (DataRow row in table.Rows)
            {
                var flight = new Flight
                    {
                        Adults = row.Field<int>("Adults"),
                        Airline = row.Field<string>("Airline"),
                        Children = row.Field<int>("Children"),
                        CityFrom = row.Field<string>("CityFrom"),
                        CityTo = row.Field<string>("CityTo"),
                        DateFrom = row.Field<DateTime>("DateFrom"),
                        DateTo = row.Field<DateTime>("DateTo"),
                        Price = row.Field<double>("Price"),
                        PriceChildren = row.Field<double>("PriceChildren"),
                        TimeFrom = row.Field<string>("TimeFrom"),
                        TimeTo = row.Field<string>("TimeTo")
                    };
                flights.Add(flight);
            }
            return flights;
        }
    }
}